﻿package com.hadron.ui.scroll {
	import com.greensock.TweenLite;
	import com.hadron.ui.scroll.behaviors.IScrollbarBehavior;
	import com.hadron.ui.scroll.behaviors.ScrollbarBehavior;
	import com.hadron.ui.scroll.events.ScrollbarEvent;
	import flash.display.DisplayObject;
	import temple.core.CoreSprite;
	import temple.debug.IDebuggable;


	/**
	 * ...
	 * @author Hadron
	 */
	public class Scrollbar extends CoreSprite implements IDebuggable
	{
		/** @private */
		private var _thumb:DisplayObject;
		/** @private */
		private var _track:DisplayObject;
		/** @private */
		private var _changeStageQuality:Boolean = false;
		/** @private */
		private var _debug:Boolean = false;
		/** @private */
		private var _scrollBehavior:IScrollbarBehavior;
		
		/**
		 * Flag changed while mouse cursor is set over an object.
		 */
		public var isMouseOver:Boolean = false;
		/**
		 * Minimum y position of a thumb
		 */
		public var yMin:Number;
		/**
		 * Maximum y position of a thumb
		 */
		public var yMax:Number;
		/**
		 * Distance between mouseY  and thumb.y position
		 */
		public var distance:Number;
		/*------------------------------------------ CONSTRUCTOR --------------------------------------------*/
		public function Scrollbar(thumb:DisplayObject, track:DisplayObject, changeStageQuality:Boolean = false) 
		{
			super();
			this._thumb = thumb;
			this._track = track;
			this._changeStageQuality = changeStageQuality;
			init();	
		}
		
		/*------------------------------------------ PUBLIC METHODS --------------------------------------------*/
		/**
		 * Updates min and max thumb position, accordingly to track height
		 */
		public function updateMinMax():void
		{
			yMin = 0;
			yMax = _track.height - _thumb.height;
			if (debug) logDebug('UPDATING MinMax yMin: ' + yMin + ' yMax: ' + yMax);
		}
		
		/**
		 * 
		 * @param	reset
		 */
		public function fixThumb(reset:Boolean = false):void
		{
			if (reset) 
			{
				calculatePercentagePosition(0);
			}
			else 
			{
				dispatchEvent(new ScrollbarEvent(_thumb.y / yMax));
			}
			if (debug) logDebug('Fixing thumb');
		}
		
		/**
		 * Resets the thumb position to 0.
		 * @param	duration Duration of reset animation
		 */
		public function resetThumbPosition(duration:Number = .5):void
		{
			TweenLite.to(_thumb, duration, { y:0 } );
			if (debug) logDebug('Resetting scrollbar position');
		}
		
		/*------------------------------------------ GETTERS & SETTERS -----------------------------------------*/
		/* INTERFACE temple.debug.IDebuggable */
		public function set debug(value:Boolean):void { _debug = value; }
		public function get debug():Boolean { return _debug; }
		/**
		 * returns track object
		 */
		public function get track():DisplayObject { return _track; }
		/**
		 * returns thumb object
		 */
		public function get thumb():DisplayObject { return _thumb; }
		/**
		 * sets changeStageQuality parameter while scrolling content,
		 * setting it to true provides better performance on slower machines
		 */
		public function set changeStageQuality(value:Boolean):void 
		{ 
			_changeStageQuality = value; 
			if (debug) logDebug('changeStageQuality: ' + _changeStageQuality);
		}
		/**
		 * returns changeStageQuality parameters
		 */
		public function get changeStageQuality():Boolean { return _changeStageQuality; }
		
		/*------------------------------------------ PRIVATE METHODS -------------------------------------------*/
		/**
		 * Inits the object after calling constructor
		 * @private
		 */
		private function init():void
		{
			//wrap _thumb with sprite container just to be sure they have button mode property
			
			var tempContainer:CoreSprite = new CoreSprite();
			tempContainer.addChild(_track);
			_track = tempContainer;
			CoreSprite(_track).buttonMode = true;
			addChild(_track);
		
			tempContainer = new CoreSprite();
			tempContainer.addChild(_thumb);
			_thumb = tempContainer;
			CoreSprite(_thumb).buttonMode = true;
			addChild(_thumb);
			updateMinMax();
			
			//init default behavior
			_scrollBehavior = new ScrollbarBehavior(this);
		}
		
		/**
		 * Calculates percentage position of rhumb relative to track height
		 * @param	mousePosition 
		 */
		public function calculatePercentagePosition(mousePosition:Number):void
		{
			//calculate relative to this.y
			if (mousePosition < 0 || mousePosition > _track.height)
			{
				trace('return');
				return;
			}
			
			var newValue:Number = mousePosition / (_track.height - _thumb.height) * (yMax - yMin);
			newValue = Math.max(yMin, Math.min(yMax, Math.round( newValue )));
			trace('new value: ' + newValue);
			_thumb.y = newValue;
			dispatchEvent(new ScrollbarEvent(_thumb.y / yMax));
		}
	}
}